package com.dianping.cat.alarm.receiver.transform;

import com.dianping.cat.alarm.receiver.entity.AlertConfig;
import com.dianping.cat.alarm.receiver.entity.Receiver;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;









public class DefaultDomParser
  extends Object
  implements IParser<Node>
{
  protected Node getChildTagNode(Node parent, String name) {
    NodeList children = parent.getChildNodes();
/*  30 */     int len = children.getLength();

    for (int i = 0; i < len; i++) {
/*  33 */       Node child = children.item(i);

/*  35 */       if (child.getNodeType() == 1 && 
        child.getNodeName().equals(name)) {
/*  37 */         return child;
      }
    }


/*  42 */     return null;
  }

  protected List<Node> getChildTagNodes(Node parent, String name) {
/*  46 */     NodeList children = parent.getChildNodes();
/*  47 */     int len = children.getLength();
/*  48 */     List<Node> nodes = new ArrayList<Node>(len);

/*  50 */     for (int i = 0; i < len; i++) {
/*  51 */       Node child = children.item(i);

/*  53 */       if (child.getNodeType() == 1 && (
/*  54 */         name == null || child.getNodeName().equals(name))) {
/*  55 */         nodes.add(child);
      }
    }


/*  60 */     return nodes;
  }

  protected Node getDocument(String xml) {
/*  64 */     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

/*  66 */     dbf.setIgnoringElementContentWhitespace(true);
/*  67 */     dbf.setIgnoringComments(true);

    try {
/*  70 */       DocumentBuilder db = dbf.newDocumentBuilder();

/*  72 */       return db.parse(new InputSource(new StringReader(xml)));
/*  73 */     } catch (Exception x) {
/*  74 */       throw new RuntimeException(x);
    }
  }

  protected List<Node> getGrandChildTagNodes(Node parent, String name) {
/*  79 */     Node child = getChildTagNode(parent, name);
/*  80 */     NodeList children = (child == null) ? null : child.getChildNodes();
/*  81 */     int len = (children == null) ? 0 : children.getLength();
/*  82 */     List<Node> nodes = new ArrayList<Node>(len);

/*  84 */     for (int i = 0; i < len; i++) {
/*  85 */       Node grandChild = children.item(i);

/*  87 */       if (grandChild.getNodeType() == 1) {
/*  88 */         nodes.add(grandChild);
      }
    }

/*  92 */     return nodes;
  }


/*  96 */   public AlertConfig parse(Node node) { return parse(new DefaultDomMaker(), new DefaultLinker(false), node); }


  public AlertConfig parse(String xml) throws SAXException, IOException {
/* 100 */     Node doc = getDocument(xml);
/* 101 */     Node rootNode = getChildTagNode(doc, "alert-config");

/* 103 */     if (rootNode == null) {
/* 104 */       throw new RuntimeException(String.format("alert-config element(%s) is expected!", new Object[] { "alert-config" }));
    }

/* 107 */     return parse(new DefaultDomMaker(), new DefaultLinker(false), rootNode);
  }

  public AlertConfig parse(IMaker<Node> maker, ILinker linker, Node node) {
/* 111 */     AlertConfig alertConfig = maker.buildAlertConfig(node);

/* 113 */     if (node != null) {
/* 114 */       AlertConfig parent = alertConfig;

/* 116 */       for (Node child : getChildTagNodes(node, "receiver")) {
/* 117 */         Receiver receiver = maker.buildReceiver(child);

/* 119 */         if (linker.onReceiver(parent, receiver)) {
/* 120 */           parseForReceiver(maker, linker, receiver, child);
        }
      }
    }

/* 125 */     return alertConfig;
  }

  public void parseForReceiver(IMaker<Node> maker, ILinker linker, Receiver parent, Node node) {
/* 129 */     for (Node child : getChildTagNodes(node, "email")) {
/* 130 */       String email = maker.buildEmail(child);

/* 132 */       parent.addEmail(email);
    }

/* 135 */     for (Node child : getChildTagNodes(node, "dx")) {
/* 136 */       String dx = maker.buildDx(child);

/* 138 */       parent.addDx(dx);
    }

/* 141 */     for (Node child : getChildTagNodes(node, "phone")) {
/* 142 */       String phone = maker.buildPhone(child);

/* 144 */       parent.addPhone(phone);
    }

/* 147 */     for (Node child : getChildTagNodes(node, "weixin")) {
/* 148 */       String weixin = maker.buildWeixin(child);

/* 150 */       parent.addWeixin(weixin);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-alarm-3.0.0.jar!/com/dianping/cat/alarm/receiver/transform/DefaultDomParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */